package com.focus.wecenter.service.impl;

import com.focus.wecenter.dao.ArticleCommentDao;
import com.focus.wecenter.dao.ArticleDao;
import com.focus.wecenter.dao.TopicDao;
import com.focus.wecenter.dao.UserDao;
import com.focus.wecenter.util.PageBean;
import com.focus.wecenter.web.vo.QuestionPublishForm;
import com.focus.wecenter.web.vo.SearchResult;
import com.focus.wecenter.service.ArticleService;
import com.focus.wecenter.pojo.Article;
import com.focus.wecenter.pojo.ArticleComment;
import com.focus.wecenter.pojo.Topic;
import com.focus.wecenter.pojo.User;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章业务实现
 *
 * @author Lv Jing
 * @date 2018年01月08日 17:18
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private TopicDao topicDao;
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private ArticleCommentDao articleCommentDao;

    @Override
    public Article publishArticle(QuestionPublishForm questionPublishForm) {
        // 关联用户
        User user = userDao.findById(Integer.parseInt(questionPublishForm.getAsk_user_id()));
        user.setIntegral(user.getIntegral() + 20);
        user.setArticleCount(user.getArticleCount() + 1);
        // 添加话题到数据库
        List<Topic> topics = new ArrayList<>();
        // 1.先检查当前话题是否已存在
        for (String topicName : questionPublishForm.getTopics()) {
            Topic topic = topicDao.findByTopicName(topicName);
            if (topic == null) {
                // 2.不存在则添加到数据库
                topic = new Topic();
                topic.setUser(user);
                topic.setTopicName(topicName);
                topicDao.add(topic);
            }
            // 3.把话题保存到临时的话题集合
            topics.add(topic);
        }

        // 添加文章
        Article article = new Article();
        article.setCreateUser(user);
        article.setContent(questionPublishForm.getContent());
        article.setTitle(questionPublishForm.getTitle());
        articleDao.add(article);

        // 文章与话题之间关系连接 FIXME:问题与话题只级联一边
        for (Topic topic : topics) {
            topic.getInvolveArticle().add(article);
        }
        // article.setInvolvedTopics(new HashSet<>(topics));
        return article;
    }

    @Override
    public Article findArticleByID(int articleID) {
        return articleDao.findById(articleID);
    }

    @Override
    public PageBean<Article> findArticleByPage(int currentPage, int pageSize) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Article.class);
        if (currentPage < 1) {
            currentPage = 1;
        }
        criteria.addOrder(Order.desc("createTime"));
        return articleDao.findByPage(criteria, currentPage, pageSize);
    }

    @Override
    public ArticleComment saveAnswerComment(String message, int userID, int articleID) {
        // 查出回答，评论数更新
        Article article = articleDao.findById(articleID);
        article.setCommentCount(article.getCommentCount() + 1);
        article.getCreateUser().setIntegral(article.getCreateUser().getIntegral() + 5);
        // 评论保存
        ArticleComment comment = new ArticleComment();
        User user = userDao.findById(userID);
        user.setIntegral(user.getIntegral() + 2);
        comment.setCreateUser(user);
        comment.setContent(message);
        article.getComments().add(comment);
        articleCommentDao.add(comment);
        return comment;
    }

    @Override
    public List<SearchResult> searchArticle(String query, int limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Article.class);
        criteria.add(Restrictions.like("title", query, MatchMode.ANYWHERE).ignoreCase());
        PageBean<Article> pageResult = articleDao.findByPage(criteria, 1, limit);
        List<SearchResult> results = new ArrayList<>();
        for (Article article : pageResult.getList()) {
            SearchResult result = new SearchResult();
            result.setId(article.getId());
            result.put("url", "article/" + article.getId());
            result.setType("articles");
            result.put("name", article.getTitle());
            result.put("comments", article.getCommentCount());
            results.add(result);
        }
        return results;
    }
}
